package com.kunpeng.weibo.tencent.api;

import java.util.ArrayList;
import java.util.List;

import com.kunpeng.weibo.tencent.beans.OAuth;
import com.kunpeng.weibo.tencent.beans.QParameter;
import com.kunpeng.weibo.tencent.utils.OAuthClient;
import com.kunpeng.weibo.tencent.utils.QHttpClient;

public class Firends_API extends Request_API {

	public Firends_API(OAuth oauth) {
		super(oauth);
	}

	/**
	 * 收听用户
	 * 
	 * @param names 要收听人的微博帐号列表（非昵称），用“,”隔开，例如：abc,bcde,effg（可选，最多30个）
	 * @param openIds 你需要读取的用户openid列表，用下划线“_”隔开，例如：
	 *        B624064BA065E01CB73F835017FE96FA_B624064BA065E01CB73F835017FE96FB
	 *        （可选，最多30个） name和fopenids至少选一个，若同时存在则以name值为主
	 */
	public String add(String[] names, String[] openIds) {
		if (mOAuth == null) {
			throw new NullPointerException();
		}
		String url = "http://open.t.qq.com/api/friends/add";
		try {
			List<QParameter> parameters = new ArrayList<QParameter>();
			parameters.add(new QParameter("format", "json"));

			StringBuffer name = new StringBuffer();
			StringBuffer openId = new StringBuffer();
			if (names != null) {
				for (int i = 0; i < names.length; i++) {
					name.append(names[i]);
					if (i < names.length - 1) {
						name.append(",");
					}
				}
				parameters.add(new QParameter("name", name.toString()));
			} else if (openIds != null) {
				for (int i = 0; i < openIds.length; i++) {
					openId.append(openIds[i]);
					if (i < openIds.length - 1) {
						openId.append("_");
					}
				}
				parameters.add(new QParameter("fopenids", openId.toString()));
			}
			parameters.addAll(mOAuth.getTokenParams());
			OAuthClient oac = new OAuthClient();
			String queryString = oac.getOauthParams(url, "POST", mOAuth.getOauth_consumer_secret(), mOAuth.getOauth_token_secret(), parameters);
			return new QHttpClient().httpPost(url, queryString);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
